package com.mongo.operation.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mongo.operation.data.DataMongoObject;
import com.mongo.operation.data.DataRepository;
import com.mongo.operation.util.ObjectUtil;
import com.mongodb.client.gridfs.model.GridFSFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;

/**
 * @author : jingling
 * @Date : 2018/10/10
 */
@Slf4j
@Service
public class GridFsMongoService {

    @Autowired
    private GridFsOperations gridFsOperations;
    @Autowired
    private DataRepository dataRepository;

    /**
     * 保存数据到gridFS中
     *
     * @param o
     * @param fillName
     */
    public void saveToGridFS(Object o, String fillName) {
        Long start = System.currentTimeMillis();
        gridFsOperations.store(ObjectUtil.objectToInputStream(o), fillName);
        log.info("保存数据到 gridFS中耗时time:{},fillName:{}", System.currentTimeMillis() - start, fillName);
    }

    /**
     * 保存JSON格式数据到gridFS中
     *
     * @param o
     * @param fillName
     */
    public void saveJsonToGridFS(Object o, String fillName) {
        saveToGridFS(JSON.toJSONString(o, SerializerFeature.WriteMapNullValue), fillName);
    }

    /**
     * 通过fillName 从gridFS中获取数据
     *
     * @param fillName 文件名
     * @return
     */
    public String getDataFromGridFSByFileName(String fillName) throws FileNotFoundException {
        Long start = System.currentTimeMillis();
        GridFSFile gridFSFile = gridFsOperations.findOne(new Query().addCriteria(Criteria.where("filename").is(fillName)));
        if (gridFSFile != null) {
//            InputStream is = gridFSFile.getInputStream();

            String result = gridFSFile.toString();
            log.info("从gridFS中获取数据耗时time:{},fillName:{}", System.currentTimeMillis() - start, fillName);
            return result;
        }
        log.info("从GridFS没有查询到数据......fillName:{}", fillName);
        return null;
    }

    /**
     * 先从默认document查询数据,没有再从gridFS查询数据
     *
     * @param fillName
     * @return
     */
    public DataMongoObject getDataByFileName(String fillName) throws FileNotFoundException {
        DataMongoObject result = dataRepository.findByToken(fillName);
        if (result == null) {
            log.info("从GridFS查询数据......fillName:{}", fillName);
            String jsonStr = getDataFromGridFSByFileName(fillName);
            if (StringUtils.isBlank(jsonStr)) {
                return null;
            }
            result = JSON.parseObject(jsonStr, DataMongoObject.class, Feature.OrderedField);
        }
        return result;
    }

}
